home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume14 / libpw / part01 next >
Encoding:
Text File  |  1990-07-15  |  12.0 KB  |  474 lines

  1. Newsgroups: comp.sources.misc
  2. From: root@lsrhs.UUCP (Mr System)
  3. Subject: v14i009: libpw, password/group file library (not YP compatible)
  4. Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5.  
  6. Posting-number: Volume 14, Issue 9
  7. Submitted-by: root@lsrhs.UUCP (Mr System)
  8. Archive-name: libpw/part01
  9.  
  10. This is a library package that helps to sift around in the password file.
  11. I have tested it thoroughly and have found no bugs on our 11/780 Ultrix 3.1.
  12. Please distribute this to everyone.. I'm sure that someone can use it.
  13. To follow, an interactive user editor should arrive to you within the
  14. next few days.    - Jim
  15.  
  16. O /        O /        O /        O /        O /
  17.  x         x         x         x         x
  18. o \        o \        o \        o \        o \
  19.  
  20. #! /bin/sh
  21. # This is a shell archive.  Remove anything before this line, then unpack
  22. # it by saving it into a file and typing "sh file".  To overwrite existing
  23. # files, type "sh file -c".  You can also feed this as standard input via
  24. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  25. # will see the following message at the end:
  26. #        "End of shell archive."
  27. # Contents:  Makefile README defs.h isuidused.c lockgrfile.c
  28. #   lockpwfile.c putgrent.c sharfile sortgrp.c sortuid.c sortuser.c
  29. #   unlockgrfile.c unlockpwfile.c
  30. # Wrapped by pulsar@lsrhs on Fri Jul 13 15:31:00 1990
  31. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  32. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  33.   echo shar: Will not clobber existing file \"'Makefile'\"
  34. else
  35. echo shar: Extracting \"'Makefile'\" \(1383 characters\)
  36. sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  37. CC          = gcc
  38. CFLAGS        = -ansi -O -W
  39. DEST          = .
  40. XEXTHDRS          = /usr/include/grp.h \
  41. X        /usr/include/pwd.h \
  42. X        /usr/include/stdio.h \
  43. X        /usr/include/sys/types.h
  44. HDRS          = defs.h
  45. LDFLAGS          = -ansi -O -W
  46. LIBS          = 
  47. LINKER          = gcc
  48. MAKEFILE      = Makefile
  49. OBJS          = isuidused.o \
  50. X        lockgrfile.o \
  51. X        lockpwfile.o \
  52. X        putgrent.o \
  53. X        sortgrp.o \
  54. X        sortuid.o \
  55. X        sortuser.o \
  56. X        unlockgrfile.o \
  57. X        unlockpwfile.o
  58. PRINT          = prnt
  59. PROGRAM          = libpw.a
  60. SRCS          = isuidused.c \
  61. X        lockgrfile.c \
  62. X        lockpwfile.c \
  63. X        putgrent.c \
  64. X        sortgrp.c \
  65. X        sortuid.c \
  66. X        sortuser.c \
  67. X        unlockgrfile.c \
  68. X        unlockpwfile.c
  69. X
  70. all:        $(PROGRAM)
  71. X
  72. X$(PROGRAM):     $(OBJS)
  73. X        @echo -n "Creating library..."
  74. X        ar cr $(PROGRAM) $(OBJS)
  75. X        ranlib $(PROGRAM)
  76. X        @echo "done"
  77. X
  78. clean:;        @rm -f $(OBJS)
  79. X
  80. depend:;    @mkmf -f $(MAKEFILE) PROGRAM=$(PROGRAM) DEST=$(DEST)
  81. X
  82. X###
  83. isuidused.o: /usr/include/stdio.h /usr/include/pwd.h \
  84. X    /usr/include/sys/types.h
  85. lockgrfile.o: /usr/include/stdio.h defs.h
  86. lockpwfile.o: /usr/include/stdio.h defs.h
  87. putgrent.o: /usr/include/stdio.h /usr/include/grp.h \
  88. X    /usr/include/sys/types.h
  89. sortgrp.o: /usr/include/stdio.h /usr/include/grp.h /usr/include/sys/types.h
  90. sortuid.o: /usr/include/stdio.h /usr/include/pwd.h /usr/include/sys/types.h
  91. sortuser.o: /usr/include/stdio.h /usr/include/pwd.h \
  92. X    /usr/include/sys/types.h
  93. unlockgrfile.o: defs.h
  94. unlockpwfile.o: defs.h
  95. END_OF_FILE
  96. if test 1383 -ne `wc -c <'Makefile'`; then
  97.     echo shar: \"'Makefile'\" unpacked with wrong size!
  98. fi
  99. # end of 'Makefile'
  100. fi
  101. if test -f 'README' -a "${1}" != "-c" ; then 
  102.   echo shar: Will not clobber existing file \"'README'\"
  103. else
  104. echo shar: Extracting \"'README'\" \(1559 characters\)
  105. sed "s/^X//" >'README' <<'END_OF_FILE'
  106. X    This is a little something I put together once I began to write
  107. password and group file editors.. If you haven't received one through the
  108. news yet, one will be coming shortly.  This is a library package, which
  109. I put in /usr/local/lib/libpw.a.  You use it by compiling with the
  110. X-lpw option.  I, personally, enjoy using gcc and the -ansi flag to ensure
  111. that this program was ansi compatible, and it is.  If you don't have gcc,
  112. you either should get it or change the Makefile.  I don't suggest touching
  113. defs.h, it was originally there for debugging purposes.  Please change the
  114. Makefile to suit your system before you make.
  115. X
  116. C subroutine    Input    Returns        Purpose / Usefulness
  117. X------------------------------------------------------------------------------
  118. isuidused(x)    int    TRUE, FALSE    Checks to see if uid x is in use.
  119. lockpwfile()    nothing    nothing        Waits for a lockfile and creates.
  120. lockgrfile()    nothing    nothing        Same as above for group file.
  121. unlockpwfile()    nothing    nothing        Removes the lock.
  122. unlockgrfile()    nothing    nothing        Removes the lock for the group file.
  123. sortgrp(grp)    char    int        Returns GID of group name 'grp'.
  124. sortuser(unm)    char    TRUE, FALSE    Checks to see if username 'unm' in use.
  125. sortuid(d)    int    int        Returns the next available UID after d.
  126. putgrent(gr)    struct    nothing        A complement to getgrent().
  127. X
  128. Since nobody ever bothered to write putgrent, I did..
  129. Sortuid(d) is a most useful tool.. You can organize the passwd file with
  130. it, instead of tacking on new users to the end.
  131. X
  132. Replies, Flames, Comments, Bugs, Gross Errors to:
  133. X
  134. X    xait.xerox.com!lsrhs!pulsar
  135. X
  136. END_OF_FILE
  137. if test 1559 -ne `wc -c <'README'`; then
  138.     echo shar: \"'README'\" unpacked with wrong size!
  139. fi
  140. chmod +x 'README'
  141. # end of 'README'
  142. fi
  143. if test -f 'defs.h' -a "${1}" != "-c" ; then 
  144.   echo shar: Will not clobber existing file \"'defs.h'\"
  145. else
  146. echo shar: Extracting \"'defs.h'\" \(86 characters\)
  147. sed "s/^X//" >'defs.h' <<'END_OF_FILE'
  148. X/*
  149. X * defs - lockfile defines
  150. X */
  151. X
  152. X#define GTMP    "/etc/gtmp"
  153. X#define PTMP    "/etc/ptmp"
  154. X
  155. END_OF_FILE
  156. if test 86 -ne `wc -c <'defs.h'`; then
  157.     echo shar: \"'defs.h'\" unpacked with wrong size!
  158. fi
  159. chmod +x 'defs.h'
  160. # end of 'defs.h'
  161. fi
  162. if test -f 'isuidused.c' -a "${1}" != "-c" ; then 
  163.   echo shar: Will not clobber existing file \"'isuidused.c'\"
  164. else
  165. echo shar: Extracting \"'isuidused.c'\" \(332 characters\)
  166. sed "s/^X//" >'isuidused.c' <<'END_OF_FILE'
  167. X/*
  168. X * isuidused(x) - if uid x is used, return 1 else 0
  169. X */
  170. X
  171. X#include <stdio.h>
  172. X#include <pwd.h>
  173. X
  174. struct passwd    *tmp;
  175. X
  176. isuidused(x)
  177. int    x;
  178. X{
  179. X    lockpwfile();
  180. X    setpwent();
  181. X
  182. X    while ((tmp = getpwent()) != NULL) {
  183. X        if (tmp->pw_uid == x) {
  184. X            unlockpwfile();
  185. X            endpwent();
  186. X            return(1);
  187. X        }
  188. X    }
  189. X    unlockpwfile();
  190. X    endpwent();
  191. X    return(0);
  192. X}
  193. END_OF_FILE
  194. if test 332 -ne `wc -c <'isuidused.c'`; then
  195.     echo shar: \"'isuidused.c'\" unpacked with wrong size!
  196. fi
  197. chmod +x 'isuidused.c'
  198. # end of 'isuidused.c'
  199. fi
  200. if test -f 'lockgrfile.c' -a "${1}" != "-c" ; then 
  201.   echo shar: Will not clobber existing file \"'lockgrfile.c'\"
  202. else
  203. echo shar: Extracting \"'lockgrfile.c'\" \(288 characters\)
  204. sed "s/^X//" >'lockgrfile.c' <<'END_OF_FILE'
  205. X/*
  206. X * lockgrfile - check for GTMP, create it when available to
  207. X */
  208. X
  209. X#include <stdio.h>
  210. X#include "defs.h"
  211. X
  212. lockgrfile()
  213. X{
  214. X    FILE    *grp;
  215. X
  216. X    while ((grp = fopen(GTMP, "r")) != NULL) {
  217. X        fclose(grp);
  218. X/*        printf("Group file is locked.  Please wait...\n"); */
  219. X        sleep(5);
  220. X    }
  221. X
  222. X    creat(GTMP, 022);
  223. X}
  224. END_OF_FILE
  225. if test 288 -ne `wc -c <'lockgrfile.c'`; then
  226.     echo shar: \"'lockgrfile.c'\" unpacked with wrong size!
  227. fi
  228. chmod +x 'lockgrfile.c'
  229. # end of 'lockgrfile.c'
  230. fi
  231. if test -f 'lockpwfile.c' -a "${1}" != "-c" ; then 
  232.   echo shar: Will not clobber existing file \"'lockpwfile.c'\"
  233. else
  234. echo shar: Extracting \"'lockpwfile.c'\" \(291 characters\)
  235. sed "s/^X//" >'lockpwfile.c' <<'END_OF_FILE'
  236. X/*
  237. X * lockpwfile - check for lockfile PTMP, when free, create it.
  238. X */
  239. X
  240. X#include <stdio.h>
  241. X#include "defs.h"
  242. X
  243. lockpwfile()
  244. X{
  245. X    FILE    *pwd;
  246. X
  247. X    while ((pwd = fopen(PTMP, "r")) != NULL) {
  248. X        fclose(pwd);
  249. X/*        printf("Password file locked.  Please wait...\n"); */
  250. X        sleep(5);
  251. X    }
  252. X
  253. X    creat(PTMP, 022);
  254. X}
  255. END_OF_FILE
  256. if test 291 -ne `wc -c <'lockpwfile.c'`; then
  257.     echo shar: \"'lockpwfile.c'\" unpacked with wrong size!
  258. fi
  259. chmod +x 'lockpwfile.c'
  260. # end of 'lockpwfile.c'
  261. fi
  262. if test -f 'putgrent.c' -a "${1}" != "-c" ; then 
  263.   echo shar: Will not clobber existing file \"'putgrent.c'\"
  264. else
  265. echo shar: Extracting \"'putgrent.c'\" \(358 characters\)
  266. sed "s/^X//" >'putgrent.c' <<'END_OF_FILE'
  267. X/*
  268. X * putgrent(fp, ent) - since there is no putgrent I'm making one.
  269. X */
  270. X
  271. X#include <stdio.h>
  272. X#include <grp.h>
  273. X
  274. putgrent(fp, grp)
  275. XFILE    *fp;
  276. struct group    *grp;
  277. X{
  278. X    int    i;
  279. X
  280. X    fprintf(fp, "%s:*:%d:%s", grp->gr_name, grp->gr_gid, grp->gr_mem[0]);
  281. X    for (i = 1; grp->gr_mem[i][0] != NULL; i++)
  282. X        fprintf(fp, ",%s", grp->gr_mem[i]);
  283. X    fprintf(fp, "\n");
  284. X    fflush(fp);
  285. X}
  286. END_OF_FILE
  287. if test 358 -ne `wc -c <'putgrent.c'`; then
  288.     echo shar: \"'putgrent.c'\" unpacked with wrong size!
  289. fi
  290. chmod +x 'putgrent.c'
  291. # end of 'putgrent.c'
  292. fi
  293. if test -f 'sharfile' -a "${1}" != "-c" ; then 
  294.   echo shar: Will not clobber existing file \"'sharfile'\"
  295. else
  296. echo shar: Extracting \"'sharfile'\" \(0 characters\)
  297. sed "s/^X//" >'sharfile' <<'END_OF_FILE'
  298. END_OF_FILE
  299. if test 0 -ne `wc -c <'sharfile'`; then
  300.     echo shar: \"'sharfile'\" unpacked with wrong size!
  301. fi
  302. # end of 'sharfile'
  303. fi
  304. if test -f 'sortgrp.c' -a "${1}" != "-c" ; then 
  305.   echo shar: Will not clobber existing file \"'sortgrp.c'\"
  306. else
  307. echo shar: Extracting \"'sortgrp.c'\" \(410 characters\)
  308. sed "s/^X//" >'sortgrp.c' <<'END_OF_FILE'
  309. X/*
  310. X * sortgrp(str) - returns the appropriate gid for group name str
  311. X */
  312. X
  313. X#include <stdio.h>
  314. X#include <grp.h>
  315. X
  316. struct group    *gr;
  317. X
  318. sortgrp(str)
  319. char    str[20];
  320. X{
  321. X    lockgrfile();
  322. X    setgrent();
  323. X    
  324. X    while ((gr = getgrent()) != NULL) {
  325. X        if (strcmp(gr->gr_name, str) == 0) { /* Compare gname */
  326. X            endgrent();
  327. X            unlockgrfile();
  328. X            return(gr->gr_gid);    /* return gid */
  329. X        }
  330. X    }
  331. X    unlockgrfile();
  332. X    endgrent();
  333. X    return(-1);
  334. X}
  335. END_OF_FILE
  336. if test 410 -ne `wc -c <'sortgrp.c'`; then
  337.     echo shar: \"'sortgrp.c'\" unpacked with wrong size!
  338. fi
  339. chmod +x 'sortgrp.c'
  340. # end of 'sortgrp.c'
  341. fi
  342. if test -f 'sortuid.c' -a "${1}" != "-c" ; then 
  343.   echo shar: Will not clobber existing file \"'sortuid.c'\"
  344. else
  345. echo shar: Extracting \"'sortuid.c'\" \(445 characters\)
  346. sed "s/^X//" >'sortuid.c' <<'END_OF_FILE'
  347. X/*
  348. X * sortuid(d) - check for next available uid after d
  349. X */
  350. X
  351. X#include <stdio.h>
  352. X#include <pwd.h>
  353. X
  354. struct passwd    *tmp;
  355. X
  356. int sortuid(d)
  357. int    d;
  358. X{
  359. X    int    i = d-1;
  360. X
  361. X    lockpwfile();
  362. X    setpwent();
  363. X
  364. X    for (;;) {
  365. X        if ((tmp = getpwent()) == NULL) {
  366. X            unlockpwfile();
  367. X            endpwent();
  368. X            return 65534;
  369. X        }
  370. X        if (tmp->pw_uid < d)
  371. X            continue;
  372. X        if (tmp->pw_uid == i+1) {
  373. X            i++;
  374. X            continue;
  375. X        } else {
  376. X            unlockpwfile();
  377. X            endpwent();
  378. X            return(++i);
  379. X        }
  380. X    }
  381. X}
  382. END_OF_FILE
  383. if test 445 -ne `wc -c <'sortuid.c'`; then
  384.     echo shar: \"'sortuid.c'\" unpacked with wrong size!
  385. fi
  386. chmod +x 'sortuid.c'
  387. # end of 'sortuid.c'
  388. fi
  389. if test -f 'sortuser.c' -a "${1}" != "-c" ; then 
  390.   echo shar: Will not clobber existing file \"'sortuser.c'\"
  391. else
  392. echo shar: Extracting \"'sortuser.c'\" \(377 characters\)
  393. sed "s/^X//" >'sortuser.c' <<'END_OF_FILE'
  394. X/*
  395. X * sortuser(uname) - look for uname in /etc/passwd.  TRUE=1, FALSE=0
  396. X */
  397. X
  398. X#include <stdio.h>
  399. X#include <pwd.h>
  400. X
  401. struct passwd *tmp;
  402. X
  403. sortuser(uname)
  404. char    uname[10];
  405. X{
  406. X    lockpwfile();
  407. X    setpwent();
  408. X
  409. X    while ((tmp = getpwent()) != NULL) {
  410. X        if (strcmp(tmp->pw_name, uname) == 0) {
  411. X            endpwent();
  412. X            unlockpwfile();
  413. X            return(1);
  414. X        }
  415. X    }
  416. X    unlockpwfile();
  417. X    endpwent();
  418. X    return(0);
  419. X}
  420. END_OF_FILE
  421. if test 377 -ne `wc -c <'sortuser.c'`; then
  422.     echo shar: \"'sortuser.c'\" unpacked with wrong size!
  423. fi
  424. chmod +x 'sortuser.c'
  425. # end of 'sortuser.c'
  426. fi
  427. if test -f 'unlockgrfile.c' -a "${1}" != "-c" ; then 
  428.   echo shar: Will not clobber existing file \"'unlockgrfile.c'\"
  429. else
  430. echo shar: Extracting \"'unlockgrfile.c'\" \(113 characters\)
  431. sed "s/^X//" >'unlockgrfile.c' <<'END_OF_FILE'
  432. X/*
  433. X * unlockgrfile - unlink GTMP to free up group file
  434. X */
  435. X
  436. X#include "defs.h"
  437. X
  438. unlockgrfile()
  439. X{
  440. X    unlink(GTMP);
  441. X}
  442. END_OF_FILE
  443. if test 113 -ne `wc -c <'unlockgrfile.c'`; then
  444.     echo shar: \"'unlockgrfile.c'\" unpacked with wrong size!
  445. fi
  446. chmod +x 'unlockgrfile.c'
  447. # end of 'unlockgrfile.c'
  448. fi
  449. if test -f 'unlockpwfile.c' -a "${1}" != "-c" ; then 
  450.   echo shar: Will not clobber existing file \"'unlockpwfile.c'\"
  451. else
  452. echo shar: Extracting \"'unlockpwfile.c'\" \(110 characters\)
  453. sed "s/^X//" >'unlockpwfile.c' <<'END_OF_FILE'
  454. X/*
  455. X * unlockpwfile - unlink the lockfile for passwd
  456. X */
  457. X
  458. X#include "defs.h"
  459. X
  460. unlockpwfile()
  461. X{
  462. X    unlink(PTMP);
  463. X}
  464. END_OF_FILE
  465. if test 110 -ne `wc -c <'unlockpwfile.c'`; then
  466.     echo shar: \"'unlockpwfile.c'\" unpacked with wrong size!
  467. fi
  468. chmod +x 'unlockpwfile.c'
  469. # end of 'unlockpwfile.c'
  470. fi
  471. echo shar: End of shell archive.
  472. exit 0
  473.  
  474.